#!/usr/bin/env python3
# _*_ coding : utf-8 _*_
import win32gui
import win32api
import win32con
from win32con import *
import win32clipboard as w
import time
import random

titles = ["",]

hwnds = []

lis = ['#签到', '#打卡']
lis1 = ['鱼子酱', '大酱汤',]


def getContentFromClipboared_u(content="寿喜锅"):

    # 打开剪贴板
    w.OpenClipboard()
    # 清空剪贴板
    w.EmptyClipboard()
    # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    w.SetClipboardData(win32con.CF_UNICODETEXT, content)
    # 获取剪贴板内容
    date = w.GetClipboardData()
    # 关闭剪贴板
    w.CloseClipboard()


def mykey_press_release_u():
    # key 1 : 0x31 or 49
    # key 2 : 0x32 or 50
    # key 3 : 0x33 or 51
    # ctrl  : 0x11 or 17
    # alt   : 0x12 or 18

    # 按下ctrl+v
    # win32api.keybd_event(0x11, 0, 0, 0)
    # win32api.keybd_event(0x56, 0, 0, 0)
    # win32api.keybd_event(0x56, 0, win32con.KEYEVENTF_KEYUP, 0)
    # win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)
    # # 按下回车
    # win32api.keybd_event(0x0D, 0, 0, 0)
    # win32api.keybd_event(0x0D, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(0x11, 0, 0, 0)
    win32api.keybd_event(0x31, 0, 0, 0)
    win32api.keybd_event(0x31, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(0x11, 0, win32con.KEYEVENTF_KEYUP, 0)


def WndProc(hwnd, msg, wParam, lParam):
    if msg == WM_PAINT:
        hdc, ps = win32gui.BeginPaint(hwnd)
        rect = win32gui.GetClientRect(hwnd)
        win32gui.DrawText(hdc, 'GUI Python', len('GUI Python'),
                          rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER)
        win32gui.EndPaint(hwnd, ps)
    if msg == WM_DESTROY:
        win32gui.PostQuitMessage(0)
    return win32gui.DefWindowProc(hwnd, msg, wParam, lParam)


def findwindowfrommouse_u():
    time.sleep(4)
    mykey_press_release_u()
    time.sleep(2)
    phw = win32gui.WindowFromPoint(win32api.GetCursorPos())
    print('phw', phw)
    print('hex(phw)', hex(phw))
    title = win32gui.GetWindowText(phw)
    print(title)
    myposition = win32gui.GetWindowRect(phw)
    print(myposition)

    # getContentFromClipboared()
    # # 打开剪贴板
    # w.OpenClipboard()
    # # 清空剪贴板
    # w.EmptyClipboard()
    # # 设置剪贴板内容
    # w.SetClipboardData(win32con.CF_UNICODETEXT, "寿喜锅")
    # # 获取剪贴板内容
    # date = w.GetClipboardData()
    # # 关闭剪贴板
    # w.CloseClipboard()

    # 显示窗口 (1347, 761) 1394, 764
    win32gui.ShowWindow(phw, win32con.SW_SHOW)
    win32api.SetCursorPos([1347, 761])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0)
    time.sleep(2)

    for i in range(8):
        getContentFromClipboared_u(random.choice(lis1) + ',')
        # 把剪切板内容粘贴到qq窗口
        win32gui.SendMessage(phw, win32con.WM_PASTE, 0, 0)
        time.sleep(1)
        # 按下后松开回车键，发送消息
        win32gui.SendMessage(phw, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        win32gui.SendMessage(phw, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
        # time.sleep(1)  # 延缓进程
        time.sleep(6)


def send(windowname: str = titles[0], msg: str = '寿喜锅'):
    title = ''
    title2 = ''
    # 打开剪贴板
    w.OpenClipboard()
    # 清空剪贴板
    w.EmptyClipboard()
    # 设置剪贴板内容
    w.SetClipboardData(win32con.CF_UNICODETEXT, msg)
    # 获取剪贴板内容
    date = w.GetClipboardData()
    # 关闭剪贴板
    w.CloseClipboard()
    # 获取qq窗口句柄
    # handle = win32gui.FindWindow(None, windowname)
    handle = win32gui.FindWindow(None, title)
    if handle == 0:
        print('未找到窗口！')
    # 显示窗口
    win32gui.ShowWindow(handle, win32con.SW_SHOW)
    # 把剪切板内容粘贴到qq窗口
    win32gui.SendMessage(handle, win32con.WM_PASTE, 0, 0)
    win32api.SetCursorPos([1394, 764])
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0)
    time.sleep(2)
    # 按下后松开回车键，发送消息
    win32gui.SendMessage(handle, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
    win32gui.SendMessage(handle, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    time.sleep(1)  # 延缓进程


if __name__ == '__main__':
    findwindowfrommouse_u()
    # send(titles[0], u'寿喜锅')
    print('Hello Python!')
